const express = require('express');
const order = express.Router();
const {Order} = require('../db/mongoose');

// 按照时间从大到小排序
function sortDataByCreateTime(a,b){
    return b.create_time - a.create_time;
}

// 获取近一年的订单
order.post('/getOneYearOrder',(req,res)=>{
    let user_id = req.body.user_id;
    
    let date = new Date().getTime() - 31536000000;
    console.log(user_id,date);

    Order.find({
        user_id:user_id,
        create_time:{$gte:date}
    },(err,data)=>{
        if(err) return res.status(200).json({
            code:'80001',
            message:'系统错误'
        })
        if(data.length == 0) return res.status(200).json({
            code:'81001',
            message:'未找到近一年的订单'
        })
        data.sort(sortDataByCreateTime);
        return res.status(200).json({
            code:'81000',
            message:'获取近一年订单成功',
            data
        })
    }).populate('shop_id').populate('food.food_id');
})

// 获取待消费订单
order.post('/getToPaidOrder',(req,res)=>{
    let user_id = req.body.user_id;

    Order.find({
        user_id:user_id,
        status:/^(待支付)$/
    },(err,data)=>{
        if(err) return res.status(200).json({
            code:'80001',
            message:'系统错误'
        })
        if(data.length == 0) return res.status(200).json({
            code:'82001',
            message:'未找到订单状态为待消费的订单'
        })
        data.sort(sortDataByCreateTime);
        return res.status(200).json({
            code:'82000',
            message:'获取待消费订单成功',
            data
        })
    }).populate('shop_id').populate('food.food_id');
})

// 获取待评价订单
order.post('/getToEvaluateOrder',(req,res)=>{
    let user_id = req.body.user_id;

    Order.find({
        user_id:user_id,
        isEvaluate:false
    },(err,data)=>{
        if(err) return res.status(200).json({
            code:'80001',
            message:'系统错误'
        })
        if(data.length == 0) return res.status(200).json({
            code:'83001',
            message:'未找到订单状态为待评价的订单'
        })
        data.sort(sortDataByCreateTime);
        return res.status(200).json({
            code:'83000',
            message:'获取待评价订单成功',
            data
        })
    }).populate('shop_id').populate('food.food_id');
})

// 获取退款订单
order.post('/getToRefundOrder',(req,res)=>{
    let user_id = req.body.user_id;

    // let reg = /.*(退款).*/;
    
    let date = new Date().getTime() - 8035200000;
    console.log(user_id,date);

    Order.find({
        user_id:user_id,
        status:/.*(退款).*/,
        create_time:{$gte:date}
    },(err,data)=>{
        if(err) return res.status(200).json({
            code:'80001',
            message:'系统错误'
        })
        if(data.length == 0) return res.status(200).json({
            code:'84001',
            message:'未找到订单状态为退款的订单'
        })
        data.sort(sortDataByCreateTime);
        return res.status(200).json({
            code:'84000',
            message:'获取退款订单成功',
            data
        })
    }).populate('shop_id').populate('food.food_id');
})

module.exports = order;